package com.keyancompany.zhongbaopingtai.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.model.TeamUser;

import java.util.List;

public class TeamUsersAdapter extends RecyclerView.Adapter<TeamUsersAdapter.ViewHolder> implements View.OnClickListener {

    private final List<TeamUser> mValues;

    public TeamUsersAdapter(List<TeamUser> items) {
        mValues = items;
    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    //define interface
    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, TeamUser user);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_teamusers, parent, false);

        ViewHolder vh = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        TeamUser user = mValues.get(position);
        holder.mItem = user;
        holder.userName.setText(user.getUsername());
        holder.email.setText(user.getEmail());
        holder.joinTime.setText(user.getJoinTime());
        if (user.getRole() != null && user.getRole().equals("创始人")) {
            holder.role.setVisibility(View.VISIBLE);
        } else {
            holder.role.setVisibility(View.GONE);
        }

        //将数据保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(user);

    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (TeamUser) v.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView userName;
        public final TextView email;
        public final TextView joinTime;
        public final ImageView role;

        public TeamUser mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            userName = (TextView) view.findViewById(R.id.txt_username);
            email = (TextView) view.findViewById(R.id.txt_email);
            joinTime = (TextView) view.findViewById(R.id.txt_jointime);
            role = (ImageView) view.findViewById(R.id.img_role);

        }

        @Override
        public String toString() {
            return super.toString() + " '" + userName.getText() + "'";
        }
    }
}